<?php

class Utils
{
	
	function checkEmailValid($email)
	{
		//$patterns = "/(.+)@(.+)\.(.+)/";
		/*$patterns = '/^(.+)'.
					 '[@]'.
					 '([^-~!@#$%^&*()_+={\\[\\]}:;"\'<,>.?\/\\|\\\\]' .
					 '[-a-z0-9]{0,62}' .
					 '[^-~!@#$%^&*()_+={\\[\\]}:;"\'<,>.?\/\\|\\\\])'.
					 '\.'.
					 '([a-z]{2}'.
					 '|com|net|edu|org'.
					 '|gov|mil|int|biz'.
					 '|pro|info|arpa|aero'.
					 '|coop|name|museum)$/ix';
		if(preg_match($patterns,$email, $matches)==1)
			return true;
		return false;*/
		return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
	}
	/**
	 * convert an input date to a mySQL date
	 *
	 * @param string $date
	 * @param string $input_format 
	 * @return mySQL date string
	 */
	static function convertToMySQLDate($date, $input_format = "%d/%m/%Y")
	{
		$date_arr = FRM_DateTime::parseDate($date,$input_format);
		if ($date_arr)
		{
			return FRM_DateTime::formatDate($date_arr, Parameter::DATE_MYSQL_FORMAT );	
		}
		return false;
	}
	
	function convertMySQLDateToOtherFormat($date, $out_format = Parameter::DATE_FORMAT_DISPLAY )
	{
		$date_arr = FRM_DateTime::parseDate($date, Parameter::DATE_MYSQL_FORMAT );
		if ($date_arr)
		{
			return FRM_DateTime::formatDate($date_arr, $out_format);	
		}
		return false;
	}
	/**
	 * subtract a date from number day
	 *
	 * @param string $date
	 * @param int $numday	number of day that you want to subtract
	 * @param string $input_format
	 * @return string date after subtract
	 */
	function subtractDate($date, $numday = 1, $input_format = "%d/%m/%Y")
	{
		$date_arr = FRM_DateTime::parseDate($date,$input_format);
		$date_int = mktime(0,0,0,$date_arr['month'], $date_arr['day'], $date_arr['year']);
		$date_int = $date_int - $numday * 86400;
		$input_format = str_replace('%','',$input_format);
		return date($input_format, $date_int);
		
	}
	
	/**
	 * add one or many day to a date 
	 *
	 * @param string $date
	 * @param int $numday	number of day that you want to add
	 * @param string $input_format
	 * @return unknown
	 */
	function addDate($date, $numday = 1, $input_format = "%d/%m/%Y")
	{
		$date_arr = FRM_DateTime::parseDate($date,$input_format);
		$date_int = mktime(0,0,0,$date_arr['month'], $date_arr['day'], $date_arr['year']);
		$date_int = $date_int + $numday * 86400;
		$input_format = str_replace('%','',$input_format);
		return date($input_format, $date_int);
		
	}
	/**
	* compare 2 date
	* @param day1 format as mysql date
	* @param day2
	* @return:
	*  			0 : equal
	*  			1 : day1 greater than day2
	*  			2 : day2 greater than day1
	*/
	static function compareDate($day1, $day2, $input_format = '%d/%m/%Y')
	{
		$day1_arr = FRM_DateTime::parseDate($day1,$input_format);
		$day2_arr = FRM_DateTime::parseDate($day2,$input_format);
		
		$day1 = mktime(0,0,0,$day1_arr['month'],$day1_arr['day'],$day1_arr['year']);
		$day2 = mktime(0,0,0,$day2_arr['month'],$day2_arr['day'],$day2_arr['year']);
		
		if ($day1 == $day2)
		{
			return 0;
		}
		elseif ($day1 > $day2)
		{
			return 1;
		}
		return 2;
	}
	
	/**
	 * check date is valid
	 *
	 * @param string $date
	 * @param  $format
	 * @return unknown
	 */
	static function isValidDate($date,$input_format = "%d/%m/%Y")
	{
		$date_arr = FRM_DateTime::parseDate($date, $input_format);
		if ($date_arr)
		{
			return checkdate($date_arr['month'], $date_arr['day'], $date_arr['year']);	
		}
		return false;
		
	}
	
	/**
	 * check whether or not current value is a positive whole integer
	 * @param string $value
	 * @return boolean
	 */
	static function isPositiveInteger($value)
	{
		return is_numeric($value) ? ( (intval($value) == $value) && ($value >= 0) ) : false;
	}
	
	public static function toI18NString($en, $vn){
	   return "<en>$en</en><vn>$vn</vn>";
	}
    
    public static function getI18NString($string, $language_code='vn'){
        $matches = array();
        if (preg_match('/<'.$language_code.'>([^*]*)<\/'.$language_code.'>/',$string,$matches) > 0)
    	{
    		return str_replace(array('&lt;','&gt;'),array('<','>'),$matches[1]);
    	}
    	return '';
    }
     static function getSumary($content, $length = 200)
    {
        $excerpt = strip_tags($content);
        $word_arr = str_word_count($excerpt, 2);
        foreach ($word_arr as $index=>$word){
            if ($index > $length){
                $length = $index;
                break;
            }
        }
        if (strlen($excerpt) > $length) {
            $excerpt = substr($excerpt,0,$length) . '...';
        }
        return $excerpt;
    }
	
}

?>